﻿using AutoMapper;
using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.Domain.Model;
using HuaHan.WorkTicket.Domain.Repositories;
using HuaHan.WorkTicket.Domain.Repositories.EntityFramework;
using HuaHan.WorkTicket.ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkTicket.Application.Implementation
{
    public class ColorGroupServiceImpl : BaseServiceImpl<ColorGroup>,IColorGroupService
    {

        #region Private Fields
        private UnitOfWork unitOfWork = new UnitOfWork();
        private GenericRepository<ColorGroupColor> colorGroupColorRepository;
        private GenericRepository<Color> colorRepository;
        #endregion

        public ColorGroupServiceImpl()
        {
            base.Repository = unitOfWork.ColorGroupRepository;
            colorGroupColorRepository = unitOfWork.ColorGroupColorRepository;
            colorRepository = unitOfWork.ColorRepository;
            base.UnitOfWork = this.unitOfWork;
        }

        public override void Add(ColorGroup entity)
        {
            Repository.Add(entity);
            foreach (var item in entity.Colors)
            {
                ColorGroupColor colorGroupColor = new ColorGroupColor();
                colorGroupColor.ColorGroupColorID = Guid.NewGuid();
                colorGroupColor.ColorGroupID = entity.ColorGroupID;
                colorGroupColor.ColorID = item.ColorID;
                colorGroupColorRepository.Add(colorGroupColor);
            }
            
            UnitOfWork.Commit();
        }

        public override ColorGroup GetItemByID(string id)
        {
            ColorGroup entity = Repository.GetByKey(Guid.Parse(id));
            entity.Colors = new List<Color>();
            foreach (var item in colorGroupColorRepository.GetAll().Where(c => c.ColorGroupID == Guid.Parse(id)))
            {
                Color color = new Color();
                color = colorRepository.GetByKey(item.ColorID);
                entity.Colors.Add(color);
            }
            return entity;
        }

        public override void Update(ColorGroup entity)
        {
            entity.UpdatedAt = DateTime.Now;
            Repository.Update(entity);

            foreach (var item in colorGroupColorRepository.GetAll().Where(c => c.ColorGroupID == entity.ColorGroupID))
            {
                colorGroupColorRepository.Delete(item.ColorGroupColorID);
            }

            foreach (var item in entity.Colors)
            {
                ColorGroupColor colorGroupColor = new ColorGroupColor();
                colorGroupColor.ColorGroupColorID = Guid.NewGuid();
                colorGroupColor.ColorGroupID = entity.ColorGroupID;
                colorGroupColor.ColorID = item.ColorID;
                colorGroupColorRepository.Add(colorGroupColor);
            }
            UnitOfWork.Commit();
        }


        public override void Delete(string id)
        {
            ColorGroup entity = Repository.GetByKey(Guid.Parse(id));
            entity.IsDeleted = true;
            entity.UpdatedAt = DateTime.Now;
            Repository.Update(entity);

            foreach (var item in colorGroupColorRepository.GetAll().Where(c=>c.ColorGroupID==entity.ColorGroupID))
            {
                item.IsDeleted = true;
                item.UpdatedAt = DateTime.Now;
                colorGroupColorRepository.Update(item);
            }

            UnitOfWork.Commit();
        }


        public bool CheckNameExisted(string name, Guid id)
        {
            var count = base.Repository.GetAll().Where(c => c.Name == name && c.ColorGroupID != id).Count();
            if (count > 0)
                return false;
            else
                return true;
        }

    }
}
